# coding=utf-8

from core.data_update import DataUpdate
from core.init.initsysparam import initsysparam
from core.masterInit import MasterInit
import webview
from core.storeinfo import StoreInfo
from webapp.config import Config
import os
from jproperties import Properties
import tools.globalvar as globalvar
from core.sqlite3db.SqliteInner import SqliteInner
from core.sqlite3db.SqliteSql import SqliteSql
from tools.OperationError import OperationError

class InitInfo():

    def init_info(self):
        '''
        登录成功初始化一些离线数据的方法
        :return:
        '''
        # 创建本地库文件
        self.createMasterDb()

        webview.evaluate_js(
            """
            vm.common.LoginStateChange({
              show: true,
              message: '初始化店仓...'
            })
            """
        )

        storeInfo = StoreInfo()
        props = storeInfo.getStoreInfo();
        print(props)
        if len(props) > 0:
            storeID = props["CP_C_STORE_ID"]
            print(storeID)
            masterinit = MasterInit()
            masterinit.masterInit(storeID)

        webview.evaluate_js(
            """
            vm.common.LoginStateChange({
              show: true,
              message: '初始化系统参数...'
            })
            """
        )
        # #获取系统参数
        # init = initsysparam()
        # init.init_sysparam()
        #初始化master全局变量
        self.createMasterObject()



        webview.evaluate_js(
            """
            vm.common.LoginStateChange({
              show: true,
              message: '开始下载数据...'
            })
            """
        )
        # 下载数据信息
        init_data = DataUpdate()
        res=init_data.Upgrade()
        # webview.evaluate_js(
        #     """
        #     vm.common.login({
        #     })
        #     """
        # )
        # res = {}
        # res["code"] = 0
        # res["message"] = "初始化完成"
        return res

    def createMasterObject(self):
        # 获取路径
        filename = Config.ROOT_PATH + os.sep + r"initconfig" + os.sep + "master.properties"
        print(filename)
        p = Properties()
        file_dir = os.path.split(filename)[0]
        if not os.path.isdir(file_dir):
            os.makedirs(file_dir)
        # 已存在文件
        if os.path.exists(filename):
            _object={}
            props = p.load(filename)
            # 全部遍历下
            for prop in props:
                _object[prop]=props[prop]
            globalvar.set_masterobject(_object)

    def createMasterDb(self):
        '''
        创建Master.db以及里面的表
        :return:
        '''
        sqliteinit = SqliteInner()
        try:
            datapath=Config.DATAROOT_PATH+os.sep+"db"
            if not os.path.isdir(datapath):
                os.makedirs(datapath)

            sqliteinit.setConnetion("master")
            # 先创建VIP表
            sql = SqliteSql.createVipTable
            cur = sqliteinit.execute(str(sql))
            cur = sqliteinit.execute(str(SqliteSql.createVipIndex))

            #创建零售相关表
            sql=SqliteSql.createRetail
            cur=sqliteinit.execute(str(sql))
            sql=SqliteSql.createRetailItem
            cur=sqliteinit.execute(str(sql))
            sql=SqliteSql.createRetailPayItem
            cur=sqliteinit.execute(str(sql))

            #创建用户表
            sqliteinit.execute(str(SqliteSql.createUsersTable))
            sqliteinit.execute(str(SqliteSql.createUsersIndex))

            #创建营业员表
            sqliteinit.execute(str(SqliteSql.createEmpsTable))
            sqliteinit.execute(str(SqliteSql.createEmpsIndex))

            sqliteinit.closedb()
        except Exception as e:
            sqliteinit.closedb()
            raise OperationError(-1, str(e.__str__()))

